package ide.ccf.ccproject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

/**
 * CCF项目性质
 * 表示CCF项目需要本插件
 *
 */
public class CCFNature implements IProjectNature
{
	private IProject project;
	public static final String NATURE_ID = "ide.ccf.ccproject.CCFNature";
	
	/**
	 * 添加CCF项目描述配置
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void configure() throws CoreException 
	{
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();
		Vector builders = new Vector();
		ICommand qmake = desc.newCommand();
		
		qmake.setBuilderName(ProjectConstants.BUILDER_ID);
		builders.add(qmake);
		
		for (int i = 0; i < commands.length; ++i) 
		{
			builders.add(commands[i]);						
		}
		
		desc.setBuildSpec((ICommand[])builders.toArray(new ICommand[builders.size()]));		
		project.setDescription(desc, null);
	}

	/**
	 * 移除CCF项目描述配置
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void deconfigure() throws CoreException 
	{
		IProjectDescription desc = getProject().getDescription();
		ICommand[] commands = desc.getBuildSpec();
		Vector builders = new Vector();
		
		for (int i = 0; i < commands.length; ++i) 
		{
			if (!commands[i].getBuilderName().equals(ProjectConstants.BUILDER_ID))
				builders.add(commands[i]);
		}
		
		desc.setBuildSpec((ICommand[])builders.toArray(new ICommand[builders.size()]));
		project.setDescription(desc, null);		
	}

	/**
	 * 获取项目
	 */
	public IProject getProject() 
	{		
		return project;
	}

	/**
	 * 设置项目
	 */
	public void setProject(IProject project)
	{
		this.project = project;
	}
	
	public static void addCCFNature(IProject project, IProgressMonitor mon) throws CoreException {
		addNature(project, ProjectConstants.NATURE_ID, mon);
	}
	
	public static void removeCCFNature(IProject project, IProgressMonitor mon) throws CoreException {
		removeNature(project, ProjectConstants.NATURE_ID, mon);
	}
	
	public static void addNature(IProject project, String natureId, IProgressMonitor monitor) throws CoreException {
	    try {
	        if (monitor == null) {
	            monitor = new NullProgressMonitor();
	        }
    		IProjectDescription description = project.getDescription();
    		String[] prevNatures = description.getNatureIds();
    		for (String prevNature : prevNatures) {
    			if (natureId.equals(prevNature))
    				return;
    		}
    		String[] newNatures = new String[prevNatures.length + 1];
    		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
    		newNatures[prevNatures.length] = natureId;
    		description.setNatureIds(newNatures);
    		project.setDescription(description, monitor);
		}

		catch (CoreException e) {
			CCorePlugin.log(e);
		}

		finally {
			monitor.done();
		}
	}
	
	public static void removeNature(IProject project, String natureId, IProgressMonitor monitor) throws CoreException {
		IProjectDescription description = project.getDescription();
		String[] prevNatures = description.getNatureIds();
		List<String> newNatures = new ArrayList<String>(Arrays.asList(prevNatures));
		newNatures.remove(natureId);
		description.setNatureIds(newNatures.toArray(new String[newNatures.size()]));
		project.setDescription(description, monitor);
	}
	
}
